package com.cjy.controller;
import com.cjy.pojo.Admin;
import com.cjy.service.adminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;


@Controller
public class AdminController {
    @Autowired
    @Qualifier("adminServiceImpl")
    private adminService adminService;

    @RequestMapping("/singin")
    public String singIN(String name, String pwd, Model model)
    {

        Admin admin = adminService.queryAdminNameAndPwd(name, pwd);
        List<Admin> list=new ArrayList<>();
        list.add(admin);
        if(name.equals(""))
        {
            model.addAttribute("errorName","用户名不能为空");
        }
        if (pwd.equals(""))
        {
            model.addAttribute("errorPwd","密码不能为空");

        }
       if(name.equals("") || pwd.equals(""))
       {
           return "singIn";
       }


       else if(admin==null)
        {
            model.addAttribute("errorSingIn","用户名或密码错误");
            return "singIn";
        }


        model.addAttribute("name", name);

        return "welcome";
    }

    @RequestMapping("/register")
    public String register(String a_name,Admin admin,Model model)
    {
        List<Admin> admins = adminService.adminByName(a_name);
        if(!admins.isEmpty())
        {
            model.addAttribute("registerError","用户名已存在请重新注册");

            return "/register";
        }
        int i = adminService.addAdmin(admin);
        if(i>0)
        {
            model.addAttribute("adminSuccess","注册成功请登录");
        }

        return "/register";
    }





}
